package cn.itsource.exam.controller;

import cn.itsource.exam.domain.Permission;
import cn.itsource.exam.query.PermissionQueryVo;
import cn.itsource.exam.service.IPermissionService;
import cn.itsource.exam.util.AjaxResult;
import cn.itsource.exam.util.PageList;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @PostMapping("/permissionList")
    public  PageList<Permission> getpermissions(@RequestBody PermissionQueryVo queryVo){
        PageList<Permission> permissionPageList= permissionService.selectPermission(queryVo);
        return permissionPageList;
    }

    @PostMapping("/permissionInfo")
    public AjaxResult add(@RequestBody Permission permission){
        if(permission==null){
            return AjaxResult.error("无法获取保存的角色信息");
        }
        try {
                Permission permission1 = permissionService.selectPermissionByName(permission);
                if (permission1 != null){
                    return AjaxResult.error("名字已重复！");
                };
                permissionService.insertPermission(permission);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @PutMapping("/permissionInfo")
    public AjaxResult update(@RequestBody Permission permission){
        if(permission==null){
            return AjaxResult.error("无法获取保存的角色信息");
        }
        try {
            permissionService.updatePermission(permission);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @DeleteMapping("/permissionInfo/{id}")
    public AjaxResult delete(@PathVariable("id") Long permissionId){
        try {
            permissionService.deletePermission(permissionId);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @PatchMapping("/permissionInfo")
    public AjaxResult batchDeleta(@RequestBody Long[] ids){
        try {
            permissionService.batchDeletePermission(ids);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }


    //获取所有权限（角色授权使用）
    @GetMapping("/permissions/{id}")
    public AjaxResult getAllPermissions(@PathVariable("id") Long id){
        try {
            List<Permission> list=permissionService.selectAllPermission();
            Map<String, Object> map = permissionService.getAllPermissionsByRoleId(id);
            return AjaxResult.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping("addRolePermission")
    public AjaxResult addRolePermission(@RequestBody String para){
        try {
            //{roleId: 4, permissionValues: [1, 3, 6]}
            JSONObject jsonObject = JSONObject.parseObject(para);
            Object roleId =  jsonObject.get("roleId");
            Long rid = Long.valueOf(roleId.toString());
            JSONArray jsonArray = (JSONArray) jsonObject.get("permissionValues");
            Object[] permissions = jsonArray.toArray();
            Long[] pids=new Long[permissions.length];
            for (int i=0;i<permissions.length;i++){
                Long pid = Long.valueOf(permissions[i].toString());
                pids[i]=pid;
            }
             permissionService.addRolePermission(rid,pids);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

}
